//
// SelectForegroundActivity.java
// ImageGenius
//
// Created by Zhang, Yifan on Jan 17, 2013
// Copyright 2013 Ming Microsystems. All rights reserved.
//

package com.mms.imagegenius;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.drawable.shapes.PathShape;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import com.mms.imagegenius.datastructure.MMSPicture;

public class SelectForegroundActivity extends Activity implements OnTouchListener{

	private MMSPicture picture = null;
	private Path path = null;
	private SelectForegroundSurface surfaceView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//TODO change this
		//this.picture = (MMSPicture) savedInstanceState.get(MMSPicture.BUNDLEKEY_STRING);
		
		//get the display metrics to fit the bitmap onto the screen
		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
		this.picture = new MMSPicture(MMSPicture.decodeSampledBitmapFromResource(getResources(), R.drawable.gal_test_img1, displayMetrics.widthPixels, displayMetrics.heightPixels));
		
		//this content view will be drawn with a Canvas
		this.surfaceView = new SelectForegroundSurface(this);
		this.surfaceView.setOnTouchListener(this);
		this.path = new Path();
		setContentView(this.surfaceView);
	}
	


	/**
	 * The onTouchListener used in hand tracing
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		Log.d("SelectForegroundActivity", "onTouch");
		float x = event.getX(); // x coord of the current event
		float y = event.getY(); // y coord of the current event

		// add the (x, y) to the path
		if(path.isEmpty()){
			path.moveTo(x, y);
		}else{
			path.lineTo(x, y);
		}

		this.updateView();
		return true; // event has been consumed.
	}

	private void updateView() {
		this.surfaceView.updateView();
	}

	PathShape getPathShape() {
		return new PathShape(this.path, 1, 1);
	}

	/*Bitmap getCompletedBitmap() {

	}*/

	class SelectForegroundSurface extends SurfaceView implements SurfaceHolder.Callback {
		SurfaceHolder holder;

		public SelectForegroundSurface(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
			holder = getHolder();
			holder.addCallback(this);
		}

		void updateView() {
			//update the view
			Canvas canvas = holder.lockCanvas();
			canvas.drawBitmap(picture.getBitmap(), 0, 0, null);
			// TODO change the properties of this paint later
			Paint paint = new Paint();
			paint.setColor(Color.BLUE);
			paint.setStyle(Style.STROKE);
			paint.setStrokeWidth(2f);
			canvas.drawPath(path, paint);
			holder.unlockCanvasAndPost(canvas);
		}
		
		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			//initialize to draw the background image after the surface has been created
		    updateView();
		}
		
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
			// TODO Auto-generated method stub
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
		}

		/*
		Bitmap combineImages(Bitmap background, Bitmap foreground, float x, float y, Context con) {
			Bitmap result = null;
			int width, height = 0;
			width = background.getWidth();
			height = background.getHeight();
			result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
			
			Canvas comboImage = new Canvas(result);
			comboImage.drawBitmap(background, 0, 0, null);
			comboImage.drawBitmap(foreground, x, y, null);
			/******
			 * 
			 * Write file to SDCard
			 * 
			 * ****
			String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
			OutputStream os = null;
			try {
				String pathis = Environment.getExternalStorageDirectory() + "/DCIM/Camera/" + tmpImg;
				os = new FileOutputStream(pathis);
				result.compress(CompressFormat.PNG, 100, os);
			} catch (IOException e) {
				Log.e("combineImages", "problem combining images", e);
			}
			return result;
		}*/
	}

	
}
